package com.giornoadd.timex.dao;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.HibernateException;

import com.giornoadd.timex.model.Department;
import com.giornoadd.timex.util.HibernateUtil;

/**
 * Manages database operations for Department table.
 * @author anil
 */
public class DepartmentManager
{
    /**
     * Returns list of all records in Department table, sorted by name.
     */
    @SuppressWarnings("unchecked")
	public List<Department> getDepartments()
    {
        List<Department> departmentList = null;

        EntityManager entityManager = HibernateUtil.getEntityManager();

        try
        {
        	entityManager.getTransaction().begin();
            departmentList = entityManager.createQuery(
                    "from Department ORDER BY name").getResultList();
            entityManager.getTransaction().commit();
        }
        catch (HibernateException e)
        {
            entityManager.getTransaction().rollback();
            throw e;
        }

        return departmentList;
    }
}
